//= require <configurable>
Spindown = Class.create(Configurable, {
    body_node_ : '',
    initialize: function(options) 
    {
        this.setOptions(options);
        this.run();
    },
    run : function()
    {
        $$('body').each(function(body){
            body.writeAttribute({'role': 'application'});
            this.body_node_ = body;
        }.bind(this));
        $$('.' + this.options.class_name + ' ' + this.options.clickable_element).each(function(element){
            element.addClassName(this.options.clickable_element_class);
            element.update('<a href="#">'+element.innerHTML+'</a>');
            element.next().hide();
        }.bind(this));
        this.body_node_.observe('click', this.checkForSpindown.bindAsEventListener(this));
    },
    checkForSpindown : function(event)
    {
        var element;
        element = event.findElement('.' + this.options.class_name + ' ' + this.options.clickable_element + ' a');
        if(element == null || element == document)
            return false;
        event.stop();
        element.up().next().toggle();
    }
});

Spindown.DEFAULT_OPTIONS = {
    class_name: 'spindown',
    clickable_element: 'h3',
    clickable_element_class: 'spindown'
};
